package com.weixin.xplore.helper;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpUtil;
import com.weixin.xplore.service.SysSettingBizService;
import com.weixin.xplore.utils.JsonUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

@Component
@RequiredArgsConstructor
public class WechatHelper {
    private final SysSettingBizService service;

    public String getOpenId(String code) {
        Map map = service.queryValueByKey("wechat.config", Map.class);
        String apppkey = MapUtil.getStr(map, "apppkey");
        String secret = MapUtil.getStr(map, "secret");
        try {
            // 构建请求微信服务器的 URL
            String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + apppkey +
                    "&secret=" + secret +
                    "&js_code=" + code +
                    "&grant_type=authorization_code";
            String s = HttpUtil.get(url);
            Map<String, Object> responseMap = JsonUtil.toMap(s, String.class, Object.class);
            if (responseMap.containsKey("openid")) {
                return responseMap.get("openid").toString();
            }
        } catch (Exception e) {
        }
        return null;
    }

    // 获取 access_token
    private String getAccessToken() {
        Map map = service.queryValueByKey("wechat.config", Map.class);
        String apppkey = MapUtil.getStr(map, "apppkey");
        String secret = MapUtil.getStr(map, "secret");
        try {
            String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + apppkey + "&secret=" + secret;
            String s = HttpUtil.get(url);


            Map<String, Object> responseMap = JsonUtil.toMap(s, String.class, Object.class);
            if (responseMap.containsKey("access_token")) {
                return (String) responseMap.get("access_token");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 发送订阅消息
    public void sendSubscribeMessage(String openId, String templateId, String page, Map<String, Map<String, String>> data) {
        try {
            // 获取 access_token
            String accessToken = getAccessToken();
            if (accessToken != null) {
                // 构建请求 URL
                String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;

                // 构建请求体
                Map<String, Object> requestBody = new HashMap<>();
                requestBody.put("touser", openId);
                requestBody.put("template_id", templateId);
                requestBody.put("page", page);
                requestBody.put("data", data);
                String post = HttpUtil.post(url, JsonUtil.toStr(requestBody));
                System.out.println("发送订阅消息响应: " + post);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
